package com.jzo2o.foundations.handler;

import com.jzo2o.api.foundations.dto.response.RegionSimpleResDTO;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.ServeCacheService;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class ActiveRegionCacheSync {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private IRegionService regionService;
    @Resource
    private ServeCacheService serveCacheService;

    @XxlJob("activeRegionCacheSync")
    public void activeRegionCacheSync(){
        //1、组装key，使用RedisTemplate删除
        String key = RedisConstants.CacheName.SERVE+"::ACTIVE_REGIONS";
        redisTemplate.delete(key);
        String serveType = RedisConstants.CacheName.SERVE_TYPE;
        redisTemplate.delete(serveType);
        //2、查询所有已启用的区域集合，会利用SpringCache将数据写入缓存
        List<RegionSimpleResDTO> regionSimpleResDTOS = regionService.queryActiveRegionListCache();
        //3、缓存每个区域的首页服务列表
        regionSimpleResDTOS.forEach(rs->{
            //3.1 组装不同区域的首页服务列表数据的key
            String home_key = RedisConstants.CacheName.SERVE_ICON+"::"+rs.getId();
            //3.2 执行缓存
            serveCacheService.firstPageServeList(rs.getId());
            //3.3 每天凌晨缓存服务类型列表
            serveCacheService.serveTypeList(rs.getId());
            //3.4 每天凌晨缓存热门服务列表
            serveCacheService.hotServeList(rs.getId());
        });
    }
}
